﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WGMClient.Properties;
using WGMClient.HelperClasses;
using System.Reflection;
using System.Threading;
using System.IO;
using WGMClient.WGMSvc;

namespace WGMClient.Views
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class SettingsView : UserControl, IMessageListener
    {
        public SettingsView()
        {
            InitializeComponent();
            HelperClasses.Registry.RegisterListener(this);
            var currDir = System.Windows.Forms.Application.StartupPath;
            var currDirName = new DirectoryInfo(currDir).Name;
            _versionTB.Text = "WGM Beta 3\nWritten by Wayne Hart\nHartlabs 2011\nv" + currDirName.ToString();
        }

        public void RestoreOptions()
        {
            try
            {
                _systemFontChooser.SelectedItem = new FontFamily( HelperClasses.Registry.Settings.SystemFontFamily);
                _systemFontColourChooser.SelectedColor = HelperClasses.Registry.Settings.SystemFontColour;
                _systemFontSizeChooser.SelectedItem = (int)HelperClasses.Registry.Settings.SystemFontSize;
                _fontBoldChk.IsChecked = HelperClasses.Registry.Settings.IsSystemFontBold;
                _fontItalicChk.IsChecked = HelperClasses.Registry.Settings.IsSystemFontItalics;
                _showDateTimeStampChk.IsChecked = HelperClasses.Registry.Settings.ShowMessageTimeStamps;
                _showImagesChk.IsChecked = HelperClasses.Registry.Settings.ShowContactImages;
                _saveGlobalHistoryChk.IsChecked =  HelperClasses.Registry.Settings.SaveGlobalHistory;   
                _savePrivateHistoryChk.IsChecked = HelperClasses.Registry.Settings.SavePrivateHistory;  
                _showOfflinePopupChk.IsChecked =   HelperClasses.Registry.Settings.ShowOfflinePopup;    
                _showOnlinePopupChk.IsChecked =    HelperClasses.Registry.Settings.ShowOnlinePopup;
                _showMessagePopupChk.IsChecked = HelperClasses.Registry.Settings.ShowNewMessagePopup;
                _useWindowAnimationsChk.IsChecked = HelperClasses.Registry.Settings.WindowAnimations;
                _windowAlwaysOnTop.IsChecked = HelperClasses.Registry.Settings.AlwaysOnTop;
                _imagePreviewHeightTb.Text = HelperClasses.Registry.Settings.ImagePreviewHeight.ToString();
                _imagePreviewWidthTb.Text = HelperClasses.Registry.Settings.ImagePreviewWidth.ToString();
                _showImagePreviewChk.IsChecked = HelperClasses.Registry.Settings.ShowImagePreviews;
                OnFontSelectionChanged(null, null);
            }
            catch { }
        }

        private void OnFontSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                _fontSampleText.FontSize = Convert.ToDouble(_systemFontSizeChooser.SelectedItem.ToString());
                _fontSampleText.Foreground = new SolidColorBrush(_systemFontColourChooser.SelectedColor);
                _fontSampleText.FontFamily = (FontFamily)_systemFontChooser.SelectedItem;
                _fontSampleText.FontWeight = _fontBoldChk.IsChecked.Value ? FontWeights.Bold : FontWeights.Normal;
                _fontSampleText.FontStyle = _fontItalicChk.IsChecked.Value ? FontStyles.Italic : FontStyles.Normal;
            }
            catch { }
        }

        private void OnFontOptionChk(object sender, RoutedEventArgs e)
        {
            OnFontSelectionChanged(null, null);
        }

        private void OnFontColourChanged(Color obj)
        {
            _systemFontColourChooser.SelectedColor = obj;
            OnFontSelectionChanged(null, null);
        }

        private void OnReset(object sender, MouseButtonEventArgs e)
        {
            RestoreOptions();
        }
        public void NotifyServerMessage(WGMSvc.WGMMessage message) { }
        public void NotifyLocalMessage(LocalMessage message)
        {
            if (message.MessageType == LocalMessageTypes.SYSTEM_OPTIONS_CHANGED)
            {
                RestoreOptions();
            }
        }

        private void OnCommit(object sender, MouseButtonEventArgs e)
        {
            try
            {
                HelperClasses.Registry.Settings.SystemFontFamily       = _fontSampleText.FontFamily.ToString()                ;
                HelperClasses.Registry.Settings.SystemFontColour       = _systemFontColourChooser.SelectedColor    ;
                HelperClasses.Registry.Settings.SystemFontSize         = _fontSampleText.FontSize                  ;
                HelperClasses.Registry.Settings.IsSystemFontBold       = _fontBoldChk.IsChecked.Value              ;
                HelperClasses.Registry.Settings.IsSystemFontItalics    = _fontItalicChk.IsChecked.Value            ;
                HelperClasses.Registry.Settings.ShowMessageTimeStamps  = _showDateTimeStampChk.IsChecked.Value     ;
                HelperClasses.Registry.Settings.ShowContactImages      = _showImagesChk.IsChecked.Value            ;
                HelperClasses.Registry.Settings.SaveGlobalHistory      = _saveGlobalHistoryChk.IsChecked.Value     ;
                HelperClasses.Registry.Settings.SavePrivateHistory     = _savePrivateHistoryChk.IsChecked.Value    ;
                HelperClasses.Registry.Settings.ShowOfflinePopup       = _showOfflinePopupChk.IsChecked.Value      ;
                HelperClasses.Registry.Settings.ShowOnlinePopup        = _showOnlinePopupChk.IsChecked.Value       ;
                HelperClasses.Registry.Settings.ShowNewMessagePopup    = _showMessagePopupChk.IsChecked.Value      ;
                HelperClasses.Registry.Settings.WindowAnimations = _useWindowAnimationsChk.IsChecked.Value;
                HelperClasses.Registry.Settings.AlwaysOnTop = _windowAlwaysOnTop.IsChecked.Value;
                HelperClasses.Registry.Settings.ShowImagePreviews = _showImagePreviewChk.IsChecked.Value;
                try
                {
                    HelperClasses.Registry.Settings.ImagePreviewWidth =  Convert.ToDouble(_imagePreviewWidthTb.Text);
                    if (HelperClasses.Registry.Settings.ImagePreviewWidth < 20)
                        HelperClasses.Registry.Settings.ImagePreviewWidth = 20;
                    if (HelperClasses.Registry.Settings.ImagePreviewWidth > 180)
                        HelperClasses.Registry.Settings.ImagePreviewWidth = 180;
                }
                catch
                {
                    HelperClasses.Registry.Settings.ImagePreviewWidth = 50;
                    _imagePreviewWidthTb.Text = "50";
                }
                try
                {
                    HelperClasses.Registry.Settings.ImagePreviewHeight = Convert.ToDouble(_imagePreviewHeightTb.Text);
                    if (HelperClasses.Registry.Settings.ImagePreviewHeight < 20)
                        HelperClasses.Registry.Settings.ImagePreviewHeight = 20;
                    if (HelperClasses.Registry.Settings.ImagePreviewHeight > 180)
                        HelperClasses.Registry.Settings.ImagePreviewHeight = 180;
                }
                catch
                {
                    HelperClasses.Registry.Settings.ImagePreviewHeight = 50;
                    _imagePreviewHeightTb.Text = "50";
                }
                HelperClasses.HelperFunctions.SaveSettings();
                HelperClasses.Registry.NotifyListeners(new WGMClient.HelperClasses.LocalMessage() { MessageType = WGMClient.HelperClasses.LocalMessageTypes.SYSTEM_OPTIONS_CHANGED });
                HelperClasses.Registry.MainWindow.SetDropDownMessage("New Options Applied.", Colors.White, Colors.SlateBlue, 3500);
                
            }
            catch
            { }
        }

        private void OnExit(object sender, MouseButtonEventArgs e)
        {
            HelperClasses.HelperFunctions.SaveSettings();
            WGMMessage leavingMsg = new WGMMessage();
            leavingMsg.SenderLogon = HelperClasses.Registry.Username;
            leavingMsg.MessageType = MessageTypes.USER_LEAVING;
            HelperClasses.Registry.Engine.SendMessageToServer(leavingMsg);
            Thread.Sleep(300);
            Registry.MainWindow.ShutdownApplication();
        }
    }
}
